package com.tevintao.marketingsystem.xposted;

import android.hardware.SensorEvent;
import android.hardware.SensorManager;

import com.tevintao.marketingsystem.log.LogManager;

import java.util.Random;

import de.robv.android.xposed.XC_MethodHook;


public class HookSensor {

    public static final String ACTION_START_SHAKE = "ACTION_START_SHAKE";
    private static HookSensor ourInstance = new HookSensor();

    public static HookSensor getInstance() {
        return ourInstance;
    }

    private HookSensor() {
    }

    public boolean startShake() {
        lastUseTime = System.currentTimeMillis();
        mStartShake = true;
        return true;
    }

    public static boolean mStartShake = false;
    public static long lastUseTime = 0;

    private int direction = 1;

    private Random mRandom = new Random();

    private XC_MethodHook sensorListener = new XC_MethodHook() {
        private int changeStep = 8;

        private float randomX = 0;
        private float randomY = 0;
        private float randomZ = 0;

        @Override
        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
            super.afterHookedMethod(param);
            if (mStartShake) {
                boolean changeDirection = mRandom.nextInt(10) > 8;
                if (changeDirection) {
                    direction = -1 * direction;
                }
                randomX = direction * mRandom.nextInt(changeStep) + mRandom.nextFloat();
                randomY = direction * mRandom.nextInt(changeStep) + mRandom.nextFloat();
                randomZ = direction * mRandom.nextInt(changeStep) + mRandom.nextFloat();
                if (param.args[0] instanceof SensorEvent) {
                    SensorEvent sensorEvent = (SensorEvent) param.args[0];
                    LogManager.getInstance().logInfo("hookPerSensor sensorListener X= " + sensorEvent.values[0] + " Y " + sensorEvent.values[1] + " Z " + sensorEvent.values[2]);

                    sensorEvent.values[0] = randomX;
                    sensorEvent.values[1] = randomY;
                    sensorEvent.values[2] = randomZ;

                    param.args[0] = sensorEvent;
                    LogManager.getInstance().logInfo("hookSensor sensorListener X= " + sensorEvent.values[0] + " Y " + sensorEvent.values[1] + " Z " + sensorEvent.values[2]);
                }
            }
            if (System.currentTimeMillis() - lastUseTime > 1000) {
                mStartShake = false;
            }
        }
    };


    private boolean register = false;

    public void hookSensor() {
        HookDeviceModule.hookAllMethodByName(SensorManager.class, "registerListener", new XC_MethodHook() {

            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                super.beforeHookedMethod(param);
                if (null != param.args[0]) {
                    LogManager.getInstance().logInfo("hookSensor args[0] = " + param.args[0]);
                    if (!register) {
                        HookDeviceModule.hookAllMethodByName(param.args[0].getClass(), "onSensorChanged", sensorListener);
                        register = true;
                    }
                }
            }
        });
    }


}
